package com.zf.rpc.loadbalancer;

import com.alibaba.nacos.api.naming.pojo.Instance;

import java.util.List;

/**
 * @ProjectName: RPC-zhaofeng
 * @Package: com.zf.rpc.loadbalancer
 * @ClassName: RoundRobinLoadBalancer
 * @Author: zf
 * @Description:
 * @Date: 2021/5/18 20:26
 * @Version: 1.0
 */
public class RoundRobinLoadBalancer implements LoadBalancer{
    private int index = 0;

    /**
     * 按照顺序依次选择第一个、第二个、第三个……这里就需要一个变量来表示当前选到了第几个：
     * @param instances
     * @return
     */
    @Override
    public Instance select(List<Instance> instances) {
        if (index >= instances.size()){
            index %= instances.size();
        }
        return instances.get(index++);
    }
}
